﻿Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports NPOI.HSSF.UserModel
Imports System.IO
Imports com.auo.flower.remote
' 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務，請取消註解下一行。
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class AssetManageWS
    Inherits System.Web.Services.WebService
    Dim dbName As String = "AUOOAMS"
    Protected FormKind As String = "AUO.FORM.87"
    Dim baDAO As OAMS.DAO.BaseDAO = New OAMS.DAO.BaseDAO()
    <WebMethod()> _
    Public Sub InsertAllMember()
        Dim db1 As OAMS.DAO.GroupDAO = New OAMS.DAO.GroupDAO(dbName)
        db1.InsertAllMember()
    End Sub


    <WebMethod()> _
    Public Sub HROrgChg()
        ''Dim amWS As AssetManageWS = New AssetManageWS()
        ''amWS.Timeout = 300 * 60 * 1000
        Return
        Dim dt As DataTable
        Dim uDao As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
        dt = uDao.GetHROrgChgData()
        For Each dr As DataRow In dt.Rows
            SendMailOther(dr("emp_no").ToString, "SYS", "8")
            uDao.InsertHEChgData(Integer.Parse(dr("form_no").ToString), dr("emp_no").ToString)
        Next

        baDAO.ErrorLog("SSIS:HROrgChg", "HROrgChg-OK", "HROrgChg", "SYS", "1", dbName)
    End Sub


    ''' <summary>
    ''' 提供介面給其他系統使用, 功能為寄出某一員工目前在公司的OA資產清單信件.外派'
    ''' </summary>
    ''' <param name="empNo">員工編號</param>
    '''  <param name="UID">操作人員</param>
    ''' <remarks>提供介面給其他系統使用, 功能為寄出某一員工目前在公司的OA資產清單信件.</remarks>
    <WebMethod()> _
    Public Sub SendMail(ByVal empNo As String, ByVal UID As String)
        SendMailOther(empNo, "SYS", "7")
    End Sub



    ''' <summary>
    ''' 提供介面給其他系統使用, 功能為寄出某一員工目前在公司的OA資產清單信件.'一般異動
    ''' </summary>
    ''' <param name="empNo">員工編號</param>
    '''  <param name="UID">操作人員</param>
    ''' <remarks>提供介面給其他系統使用, 功能為寄出某一員工目前在公司的OA資產清單信件.</remarks>
    <WebMethod()> _
    Public Sub SendMailBy1(ByVal empNo As String, ByVal UID As String)


        Dim bbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()

        Dim abbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        '取得符合條件的資產合計資料
        Dim ds As DataSet = abbl.GetAssetGroupData("", "", "", "0", "", "", empNo, "", dbName)

        Dim MailType As String = System.Configuration.ConfigurationManager.AppSettings("MailType")

        Dim email, MailTo As String
        MailTo = System.Configuration.ConfigurationManager.AppSettings("MailTo")
        Dim MailFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
        Dim SMTPServer As String = System.Configuration.ConfigurationManager.AppSettings("SMTPServer")

        'Dim rootDir As String = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/")
        Dim rootDir As String = System.Configuration.ConfigurationManager.AppSettings("rootDir")
        Dim drs() As DataRow
        Dim WebReply As String = System.Configuration.ConfigurationManager.AppSettings("WebReply")
        Dim WebQuery As String = System.Configuration.ConfigurationManager.AppSettings("WebQuery")
        Dim uBBl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        Dim mParBBl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim drParam As DataRow = mParBBl.GetMailParamByID("1", dbName).Rows(0)

        ''先送信
        For Each dr As DataRow In ds.Tables(1).Rows '某一個員工
            drs = ds.Tables(0).Select("keeping_person='" + dr("keeping_person").ToString.Trim + "'")
            If MailType.ToLower <> "online" Then '測試
                email = MailTo
            Else '上線
                email = drs(0)("EMail").ToString()
            End If
            '送出信件
            ''配合語文別
            Select Case uBBl.GetLanguage(dr("keeping_person").ToString.ToLower(), dbName)
                Case "en-us"
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailSubjectEng").ToString, drParam("MailBodyEng").ToString(), dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "en-us")
                Case "zh-cn"
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailsubjectChn").ToString, drParam("MailBodyChn").ToString, dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-cn")
                Case Else
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailSubject").ToString, drParam("MailBody").ToString, dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-tw")
            End Select
        Next
        Dim ResendDays As Integer = Integer.Parse(drParam("ResendDays").ToString)
        Dim ResendTimes As Integer = Integer.Parse(drParam("ResendTimes").ToString)
        Dim ReturnDays As Integer = Integer.Parse(drParam("ReturnDays").ToString)
        Dim ReturnDeadLine As DateTime

        ''再新增紀錄到資料庫中(清除已回報資料及新增LOG)
        Dim asDS As DataSet
        For Each dr1 As DataRow In ds.Tables(1).Rows ''每一個UID
            asDS = abbl.GetAssetDataByAssettypeUID("", dr1("keeping_person").ToString, dbName) '找出某一個人的所有資產

            ReturnDeadLine = abbl.GetWorkDate(Today, ReturnDays, dr1("keeping_person").ToString, dbName)
            For Each dr2 As DataRow In asDS.Tables(0).Rows
                Select Case uBBl.GetLanguage(dr1("keeping_person").ToString.ToLower(), dbName)
                    Case "en-us"
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailSubjectEng").ToString, drParam("MailBodyEng").ToString, 0, dbName) ''逐筆寫入資料

                    Case "zh-tw"
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailSubject"), drParam("MailBody").ToString, 0, dbName) ''逐筆寫入資料

                    Case Else
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailsubjectCht").ToString, drParam("MailBodyChn").ToString, 0, dbName) ''逐筆寫入資料


                End Select

            Next

        Next



    End Sub
    <WebMethod()> _
   Public Sub SendMailOther(ByVal empNo As String, ByVal UID As String, ByVal ParamID As String)


        Dim bbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()

        Dim abbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        '取得符合條件的資產合計資料
        Dim ds As DataSet = abbl.GetAssetGroupData("", "", "", "0", "", "", empNo, "", dbName)

        Dim MailType As String = System.Configuration.ConfigurationManager.AppSettings("MailType")

        Dim email, MailTo As String
        MailTo = System.Configuration.ConfigurationManager.AppSettings("MailTo")
        Dim MailFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
        Dim SMTPServer As String = System.Configuration.ConfigurationManager.AppSettings("SMTPServer")

        'Dim rootDir As String = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/")
        Dim rootDir As String = System.Configuration.ConfigurationManager.AppSettings("rootDir")
        Dim drs() As DataRow
        Dim WebReply As String = System.Configuration.ConfigurationManager.AppSettings("WebReply")
        Dim WebQuery As String = System.Configuration.ConfigurationManager.AppSettings("WebQuery")
        Dim uBBl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        Dim mParBBl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim drParam As DataRow = mParBBl.GetMailParamByID(ParamID, dbName).Rows(0)

        ''先送信
        For Each dr As DataRow In ds.Tables(1).Rows '某一個員工
            drs = ds.Tables(0).Select("keeping_person='" + dr("keeping_person").ToString.Trim + "'")
            If MailType.ToLower <> "online" Then '測試
                email = MailTo
            Else '上線
                email = drs(0)("EMail").ToString()
            End If
            '送出信件
            ''配合語文別
            Select Case uBBl.GetLanguage(dr("keeping_person").ToString.ToLower(), dbName)
                Case "en-us"
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailSubjectEng").ToString, drParam("MailBodyEng").ToString(), dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "en-us")
                Case "zh-cn"
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailsubjectChn").ToString, drParam("MailBodyChn").ToString, dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-cn")
                Case Else
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailSubject").ToString, drParam("MailBody").ToString, dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-tw")
            End Select
        Next
        Dim ResendDays As Integer = Integer.Parse(drParam("ResendDays").ToString)
        Dim ResendTimes As Integer = Integer.Parse(drParam("ResendTimes").ToString)
        Dim ReturnDays As Integer = Integer.Parse(drParam("ReturnDays").ToString)
        Dim ReturnDeadLine As DateTime

        ''再新增紀錄到資料庫中(清除已回報資料及新增LOG)
        Dim asDS As DataSet
        For Each dr1 As DataRow In ds.Tables(1).Rows ''每一個UID
            asDS = abbl.GetAssetDataByAssettypeUID("", dr1("keeping_person").ToString, dbName) '找出某一個人的所有資產

            ReturnDeadLine = abbl.GetWorkDate(Today, ReturnDays, dr1("keeping_person").ToString, dbName)
            For Each dr2 As DataRow In asDS.Tables(0).Rows
                Select Case uBBl.GetLanguage(dr1("keeping_person").ToString.ToLower(), dbName)
                    Case "en-us"
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailSubjectEng").ToString, drParam("MailBodyEng").ToString, 0, dbName) ''逐筆寫入資料

                    Case "zh-tw"
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailSubject"), drParam("MailBody").ToString, 0, dbName) ''逐筆寫入資料

                    Case Else
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailsubjectCht").ToString, drParam("MailBodyChn").ToString, 0, dbName) ''逐筆寫入資料


                End Select

            Next

        Next



    End Sub
    ''' <summary>
    ''' 匯入IP資料及Login資料
    ''' </summary>
    ''' <remarks></remarks>
    <WebMethod()> _
    Public Sub ImportData()
        ImportIP()
        ImportLogin()
        baDAO.ErrorLog("SSIS:ImportData", "ImportData-OK", "WSImportData", "SYS", "1", dbName)
    End Sub

    Private Sub ImportLogin()
        Dim loginDao As AssetManage.DAO.LoginDataDAO = New AssetManage.DAO.LoginDataDAO(dbName)
        Dim IPDataPath As String = ""
        IPDataPath = loginDao.GetDataPath()
        Dim body As String = ""
        Dim ds As New DataSet()

        If IPDataPath <> "" Then

            ''=========================================================================

            Dim ExcelImportCheck As ExcelImportCheck
            Dim dt As DataTable

            Dim headerRow As HSSFRow
            Dim row As DataRow
            Dim cellCount As Integer
            Dim site, location, ip1, ip2 As String
            Dim dirInfo As New DirectoryInfo(IPDataPath)

            For Each myFileInfo As FileInfo In dirInfo.GetFiles()
                ExcelImportCheck = New ExcelImportCheck()
                dt = New DataTable
                Try
                    If ExcelImportCheck.LoadData(dt, myFileInfo.FullName, "Sheet1", False) = False Then
                        Continue For
                    End If
                Catch ex As Exception
                    Continue For
                End Try



                dt.Columns(0).ColumnName = "Site"
                dt.Columns(1).ColumnName = "ComputerName"
                dt.Columns(2).ColumnName = "IPAddress"
                dt.Columns(3).ColumnName = "MACAddress"
                dt.Columns(5).ColumnName = "LoginID"
                dt.Columns(11).ColumnName = "LoginDate"
                dt.Columns.Add("IPCount")
                dt.Columns.Add("MACCount")
                For i As Integer = 0 To dt.Rows.Count - 1
                    dt.Rows(i)("Site") = dt.Rows(i)("Site").ToString().ToUpper().Replace("-SITE", "")

                    dt.Rows(i)("IPAddress") = dt.Rows(i)("IPAddress").ToString().Replace("""", "")
                    If dt.Rows(i)("IPAddress").ToString.Contains("0.0.0.0,") Then
                        dt.Rows(i)("IPAddress") = dt.Rows(i)("IPAddress").ToString.Replace("0.0.0.0,", "")
                    ElseIf dt.Rows(i)("IPAddress").ToString.Contains(",0.0.0.0") Then
                        dt.Rows(i)("IPAddress") = dt.Rows(i)("IPAddress").ToString.Replace(",0.0.0.0", "")
                    End If

                    If dt.Rows(i)("IPAddress").ToString.Contains(",") Then
                        dt.Rows(i)("IPCount") = dt.Rows(i)("IPAddress").ToString.Split(",").Length
                    Else
                        dt.Rows(i)("IPCount") = 1
                    End If

                    dt.Rows(i)("MACAddress") = dt.Rows(i)("MACAddress").ToString().Replace("""", "")
                    If dt.Rows(i)("MACAddress").ToString.Contains("00:00:00:00:00:00,") Then
                        dt.Rows(i)("MACAddress") = dt.Rows(i)("MACAddress").ToString.Replace("00:00:00:00:00:00,", "")
                    ElseIf dt.Rows(i)("MACAddress").ToString.Contains(",00:00:00:00:00:00") Then
                        dt.Rows(i)("MACAddress") = dt.Rows(i)("MACAddress").ToString.Replace(",00:00:00:00:00:00", "")
                    End If

                    If dt.Rows(i)("MACAddress").ToString.Contains(",") Then
                        dt.Rows(i)("MACCount") = dt.Rows(i)("MACAddress").ToString.Split(",").Length
                    Else
                        dt.Rows(i)("MACCount") = 1
                    End If

                Next
                Dim j As Integer
                For i As Integer = 0 To dt.Rows.Count - 1
                    j = 0
                    If dt.Rows(i)("MACCount") > dt.Rows(i)("IPCount") Then
                        For Each strMac As String In dt.Rows(i)("MACAddress").ToString().Split(",")
                            If dt.Rows(i)("IPCount") > 1 Then
                                If j = dt.Rows(i)("IPCount") Then
                                    j = j - 1
                                ElseIf j = 0 Then
                                    j = 0
                                Else
                                    j = j + 1
                                End If
                                loginDao.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Split(",")(j).Trim(), strMac.Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), "Sys", dt.Rows(i)("ComputerName").ToString().Trim())

                            Else
                                loginDao.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Trim(), strMac.Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), "Sys", dt.Rows(i)("ComputerName").ToString().Trim())
                            End If

                        Next
                    ElseIf dt.Rows(i)("IPCount") > dt.Rows(i)("MACCount") Then
                        For Each strIP As String In dt.Rows(i)("IPAddress").ToString().Split(",")

                            If dt.Rows(i)("MACCount") > 1 Then
                                If j = dt.Rows(i)("MACCount") Then
                                    j = j - 1
                                ElseIf j = 0 Then
                                    j = 0
                                Else
                                    j = j + 1
                                End If
                                loginDao.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), strIP.Trim(), dt.Rows(i)("MACAddress").ToString().Split(",")(j).Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), "Sys", dt.Rows(i)("ComputerName").ToString().Trim())
                            Else
                                loginDao.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), strIP.Trim(), dt.Rows(i)("MACAddress").ToString().Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), "Sys", dt.Rows(i)("ComputerName").ToString().Trim())
                            End If


                        Next

                    ElseIf dt.Rows(i)("MACCount") = 1 AndAlso dt.Rows(i)("IPCount") = 1 Then
                        loginDao.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Trim(), dt.Rows(i)("MACAddress").ToString().Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), "Sys", dt.Rows(i)("ComputerName").ToString().Trim())
                    ElseIf dt.Rows(i)("MACCount") = dt.Rows(i)("IPCount") AndAlso dt.Rows(i)("MACCount") > 1 Then

                        For Each strMac As String In dt.Rows(i)("MACAddress").ToString().Split(",")
                            loginDao.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Split(",")(j).Trim(), strMac.Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), "Sys", dt.Rows(i)("ComputerName").ToString().Trim())
                            j = j + 1
                        Next
                    End If
                Next

                ''=======================================================================
                Directory.CreateDirectory(IPDataPath + "\Bak\" + Today.ToString("yyyyMMdd"))
                File.Move(myFileInfo.FullName, IPDataPath + "\Bak\" + Today.ToString("yyyyMMdd") + myFileInfo.Name)

            Next

        End If
    End Sub


    Private Sub ImportIP()
        Dim IPDao As AssetManage.DAO.IPLocationDataDAO = New AssetManage.DAO.IPLocationDataDAO(dbName)
        Dim IPDataPath As String = ""
        IPDataPath = IPDao.GetDataPath()
        Dim body As String = ""
        Dim ds As New DataSet()

        If IPDataPath <> "" Then

            ''=========================================================================

            Dim ExcelImportCheck As ExcelImportCheck
            Dim dt As DataTable

            Dim headerRow As HSSFRow
            Dim row As DataRow
            Dim cellCount As Integer
            Dim site, location, ip1, ip2 As String
            Dim dirInfo As New DirectoryInfo(IPDataPath)

            For Each myFileInfo As FileInfo In dirInfo.GetFiles()
                ExcelImportCheck = New ExcelImportCheck()
                dt = New DataTable
                Try
                    If ExcelImportCheck.LoadData(dt, myFileInfo.FullName, "AUHY", False) = False Then
                        Continue For
                    End If
                Catch ex As Exception
                    Continue For
                End Try


                dt.Columns(0).ColumnName = "Site"
                dt.Columns(1).ColumnName = "Location"

                dt.Columns(2).ColumnName = "IPBeg"

                dt.Columns(3).ColumnName = "IPEnd"
                For ii As Integer = 0 To dt.Rows.Count - 1
                    row = dt.Rows(ii)
                    site = row("Site").ToString.Trim
                    location = row("Location").ToString.Trim
                    ip1 = row("IPBeg").ToString.Trim
                    ip2 = row("IPEnd").ToString.Trim

                    If Me.IPCheck(ip1, ip2, location, site) = False Then

                    Else
                        If site.Trim = "" Then
                            Continue For
                        End If
                        IPDao.InsertIPLocationData(site, location, ip1, ip2, "Sys")

                    End If
                Next


                ''=======================================================================
                Directory.CreateDirectory(IPDataPath + "\Bak\" + Today.ToString("yyyyMMdd"))
                File.Move(myFileInfo.FullName, IPDataPath + "\Bak\" + Today.ToString("yyyyMMdd") + myFileInfo.Name)

            Next

        End If
    End Sub
    Protected Function IPCheck(ByVal IP_Beg As String, ByVal IP_End As String, ByVal Location As String, ByVal Site As String) As Boolean
        Dim bbl As AssetManage.DAO.IPLocationDataDAO = New AssetManage.DAO.IPLocationDataDAO(dbName)
        If bbl.IPExist(IP_Beg, IP_End, Location, Site) Then
            Return False
        End If
        Return True
    End Function




    'Const MailType As String = "TrailRun"
    'Const MailTo As String = "andy.wang@creasys.com.tw"
    'Const MailFrom As String = "andy.wang@creasys.com.tw"
    'Const SMTPServer As String = "PC01"


    'Const WebReply As String = "http://localhost:52439/AssetManage/WebForm/Main/MainPage.aspx?path=http://localhost:52439/AssetManage/webForm/AssetMaintain/AssetMaintain.aspx"
    'Const WebQuery As String = "http://localhost:52439/AssetManage/WebForm/Main/MainPage.aspx?path=http://localhost:52439/AssetManage/webForm/AssetMaintain/AssetMaintain.aspx"


    Dim MailType As String = System.Configuration.ConfigurationManager.AppSettings("MailType")


    Dim MailTo = System.Configuration.ConfigurationManager.AppSettings("MailTo")
    Dim MailFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
    Dim SMTPServer As String = System.Configuration.ConfigurationManager.AppSettings("SMTPServer")

    '  Dim rootDir As String = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/")
    Dim rootDir As String = System.Configuration.ConfigurationManager.AppSettings("rootDir")
    Dim WebReply As String = rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + System.Configuration.ConfigurationManager.AppSettings("WebReply")
    Dim WebQuery As String = rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + System.Configuration.ConfigurationManager.AppSettings("WebQuery")


    <WebMethod()> _
   Public Sub SendMailWSSchdule()
        Try
            SendType1()
            SendType2()
            SendType3()
            SendType4()
            '''檢查是否有重送的信件
            ResendMail()
            ''寄送生日信件
            '  SendBitthDayMail()
            ''將目前尚在簽核中的資料更新
            UpdateApproveData()
            ''將log整合
            RefreshAssetLog()


            baDAO.ErrorLog("SSIS:SendMailWSSchdule", "SendMailWSSchdule-OK", "SendMailWSSchdule", "SYS", "1", dbName)
        Catch ex As Exception
            baDAO.ErrorLog("SSIS:SendMailWSSchdule", "SendMailWSSchdule-NotOK:" + ex.Message, "SendMailWSSchdule", "SYS", "0", dbName)
        End Try

    End Sub

    <WebMethod()> _
      Public Sub RefreshAssetLog()
        Dim dt As DataTable
        Dim assDao As AssetManage.DAO.AssetDAO = New AssetManage.DAO.AssetDAO(dbName)
        dt = assDao.GetAssetLogFromOldSystem()
        Dim modiType As String = ""
        For Each dr As DataRow In dt.Rows
            Try
                If dr("ModiType").ToString = "I" Then
                    modiType = "0"
                Else
                    modiType = "1"
                End If
                assDao.InsertAssetLogByOldSystem(dr, modiType)
                assDao.UpdateOldSystemLog(Integer.Parse(dr("Id").ToString))

            Catch ex As Exception
                baDAO.ErrorLog("SSIS:RefreshAssetLog", "RefreshAssetLog-NotOK:" + ex.Message, "RefreshAssetLog", "SYS", "0", dbName)
            End Try
        Next
        baDAO.ErrorLog("SSIS:RefreshAssetLog", "RefreshAssetLog-OK", "RefreshAssetLog", "SYS", "1", dbName)
    End Sub

    <WebMethod()> _
   Public Sub UpdateApproveData()
        Dim dt As DataTable
        Dim assDao As AssetManage.DAO.AssetDAO = New AssetManage.DAO.AssetDAO(dbName)
        dt = assDao.GetApprovingData()
        Dim helper As IRemotingFormQueryHelper = RemotingHelperCreator.CreateInstance(GetType(IRemotingFormQueryHelper))
        Dim status As String = ""
        '狀態值(狀態描述)
        'NC(未完成)
        'SA(已儲存)
        'WA(待簽核)
        'UA(簽核中)
        'WP(待處理)
        'AP(已處理)
        'RJ(退回(簽核人否決))
        'RF(退回(處理人否決))
        'CL(已結案)
        'RC(撤回)
        'HT(歷史檔)
        'IM(變更中)
        'DE(已刪除)


        For Each dr As DataRow In dt.Rows
            Try
                status = helper.getFormStatus(FormKind, Integer.Parse(dr("ApplyFormNo").ToString)).FORM_STATUS.ToLower.Trim

                If status <> "approving" And status <> "nc" And status <> "wa" And status <> "ua" And status <> "wp" And status <> "im" Then ''不在簽核中的資料要清除旗標
                    assDao.ClearApprovingData(dr("asset_serialNo").ToString)
                End If
            Catch ex As Exception
                baDAO.ErrorLog("SSIS:UpdateApproveData", "UpdateApproveData-NotOK:" + ex.Message, "UpdateApproveData", "SYS", "0", dbName)
            End Try


        Next
        baDAO.ErrorLog("SSIS:UpdateApproveData", "UpdateApproveData-OK", "UpdateApproveData", "SYS", "1", dbName)
    End Sub
    Private Sub ResendMail()


        Dim dsGroup As DataSet
        Dim mbbl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim maBBL As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()
        Dim aBBL As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        Dim email, MailSubject, MailBody, Language As String
        Dim drs() As DataRow
        Dim dr As DataRow
        Dim ReturnDeadLine As DateTime
        Dim ReturnDays, ResendDays, ResendTimes As Integer
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        dr = mbbl.GetMailParamByID("9", dbName).Rows(0)
        dsGroup = aBBL.GetAssetResendData(dbName)
        For Each dr1 As DataRow In dsGroup.Tables(1).Rows
            drs = dsGroup.Tables(0).Select("keeping_person='" + dr1("keeping_person").ToString.Trim + "'")
            Language = uBBL.GetLanguage(dr1("keeping_person").ToString.Trim, dbName)

            Select Case Language
                Case "en-us"
                    MailSubject = dr("MailSubjectEng").ToString()
                    MailBody = dr("MailBodyEng").ToString()
                Case "zh-cn"
                    MailSubject = dr("MailSubjectChn").ToString()
                    MailBody = dr("MailBodyChn").ToString()
                Case Else
                    MailSubject = dr("MailSubject").ToString()
                    MailBody = dr("MailBody").ToString()
            End Select

            If MailType.ToLower <> "online" Then
                email = MailTo
            Else
                email = drs(0)("EMail").ToString()
            End If

            maBBL.SendMailDataSum(drs, WebReply, MailFrom, SMTPServer, email, MailSubject, MailBody, dbName, "", WebQuery, Language)
        Next




        For Each dr3 As DataRow In dsGroup.Tables(1).Rows

            Language = uBBL.GetLanguage(dr3("keeping_person").ToString.Trim, dbName)

            Select Case Language
                Case "en-us"
                    MailSubject = dr("MailSubjectEng").ToString()
                    MailBody = dr("MailBodyEng").ToString()
                Case "zh-cn"
                    MailSubject = dr("MailSubjectChn").ToString()
                    MailBody = dr("MailBodyChn").ToString()
                Case Else
                    MailSubject = dr("MailSubject").ToString()
                    MailBody = dr("MailBody").ToString()
            End Select
            ReturnDeadLine = aBBL.GetWorkDate(Today, ReturnDays, dr3("keeping_person").ToString, dbName)
            For Each dr2 As DataRow In dsGroup.Tables(2).Rows
                aBBL.InsertReSendMailLog(dr2("asset_serialNo").ToString, "Sys", dbName)
            Next


        Next





    End Sub





    Private Sub SendType1()
        '抓出設定檔
        Dim dt As DataTable
        Dim dbName As String = "AUOOAMS"
        Dim dsGroup As DataSet
        Dim mbbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()
        Dim aBBL As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        Dim AssetType, DeptNo, TakeOfficeDate, HouseID, TopN, EquQty As String
        Dim dd As DateTime

        Dim email, MailBody, MailSubject, Language As String

        Dim drs() As DataRow
        Dim asDS As DataSet
        Dim ReturnDeadLine As DateTime
        Dim ReturnDays, ResendDays, ResendTimes As Integer
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()

        dt = mbbl.GetSendMail("", "", "", dbName)
        For Each dr As DataRow In dt.Rows
            If dr("ScheduleType").ToString = "1" And (dr("Status").ToString = "N" Or dr("Status").ToString = "P") Then

                If dr("Dayst").ToString <> "32" Then '不是月底
                    dd = DateTime.Parse(Today.Year.ToString + "/" + dr("Monthst").ToString + "/" + dr("Dayst").ToString)
                Else '是月底
                    dd = DateTime.Parse(Today.Year.ToString + "/" + dr("Monthst").ToString + "/01").AddMonths(1).AddDays(-1)
                End If

                If dd = Today Then

                    AssetType = dr("AssetType").ToString()
                    DeptNo = dr("DeptNo1").ToString()
                    TakeOfficeDate = dr("TakeOfficeDate").ToString()
                    HouseID = dr("HouseID").ToString()
                    TopN = dr("TopN").ToString()
                    EquQty = dr("EquQty").ToString()

                    ResendDays = Integer.Parse(dr("ResendDays").ToString)
                    ResendTimes = Integer.Parse(dr("ResendTimes").ToString)
                    ReturnDays = Integer.Parse(dr("ReturnDays").ToString)

                    dsGroup = aBBL.GetAssetGroupData(AssetType, DeptNo.TrimEnd("0"), TakeOfficeDate, HouseID, TopN, EquQty, "", "", dbName)
                    For Each dr1 As DataRow In dsGroup.Tables(1).Rows '某一個員工
                        drs = dsGroup.Tables(0).Select("keeping_person='" + dr1("keeping_person").ToString.Trim + "'")
                        Language = uBBL.GetLanguage(dr1("keeping_person").ToString.Trim, dbName)


                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select

                        If MailType.ToLower <> "online" Then
                            email = MailTo
                        Else
                            email = drs(0)("EMail").ToString()
                        End If

                        mbbl.SendMailDataSum(drs, WebReply, MailFrom, SMTPServer, email, MailSubject, MailBody, dbName, "", WebQuery, Language)
                    Next

                    ''再新增紀錄到資料庫中(清除已回報資料及新增LOG)

                    For Each dr3 As DataRow In dsGroup.Tables(1).Rows ''每一個UID
                        asDS = aBBL.GetAssetDataByAssettypeUID("", dr3("keeping_person").ToString, dbName) '找出某一個人的所有資產
                        Language = uBBL.GetLanguage(dr3("keeping_person").ToString.Trim, dbName)

                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select
                        ReturnDeadLine = aBBL.GetWorkDate(Today, ReturnDays, dr3("keeping_person").ToString, dbName)
                        For Each dr2 As DataRow In asDS.Tables(0).Rows
                            aBBL.InsertSendMailLogWS(dr2("asset_serialNo").ToString, "Sys", ReturnDeadLine, ResendDays, ResendTimes, MailSubject, MailBody, Integer.Parse(dr("SerNo").ToString), dbName) ''逐筆寫入資料
                        Next

                    Next
                    mbbl.UpdateSendMailStatus(dr("SerNo").ToString, "P", dbName, "Sys")
                End If

            End If


        Next


    End Sub


    Private Sub SendType2()
        '抓出設定檔
        Dim dt As DataTable
        Dim dbName As String = "AUOOAMS"
        Dim dsGroup As DataSet
        Dim mbbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()
        Dim aBBL As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        Dim AssetType, DeptNo, TakeOfficeDate, HouseID, TopN, EquQty As String
        Dim dd1, dd2, dd3, dd4 As DateTime
        Dim MailSubject, MailBody, Language As String
        Dim email As String

        Dim drs() As DataRow

        Dim asDS As DataSet
        Dim ReturnDeadLine As DateTime
        Dim ReturnDays, ResendDays, ResendTimes As Integer
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        dt = mbbl.GetSendMail("", "", "", dbName)
        For Each dr As DataRow In dt.Rows
            If dr("ScheduleType").ToString = "2" And (dr("Status").ToString = "N" Or dr("Status").ToString = "P") Then '每季
                ''找出是今日的資料
                If dr("Monthst").ToString = "1" Then
                    If dr("Dayst").ToString <> "32" Then '不是月底
                        dd1 = DateTime.Parse(Today.Year.ToString + "/01/" + dr("Dayst").ToString)
                        dd2 = DateTime.Parse(Today.Year.ToString + "/04/" + dr("Dayst").ToString)
                        dd3 = DateTime.Parse(Today.Year.ToString + "/07/" + dr("Dayst").ToString)
                        dd4 = DateTime.Parse(Today.Year.ToString + "/10/" + dr("Dayst").ToString)

                    Else '是月底
                        dd1 = DateTime.Parse(Today.Year.ToString + "/01/01").AddMonths(1).AddDays(-1)
                        dd2 = DateTime.Parse(Today.Year.ToString + "/04/01").AddMonths(1).AddDays(-1)
                        dd3 = DateTime.Parse(Today.Year.ToString + "/07/01").AddMonths(1).AddDays(-1)
                        dd4 = DateTime.Parse(Today.Year.ToString + "/10/01").AddMonths(1).AddDays(-1)

                    End If
                End If
                If dr("Monthst").ToString = "2" Then
                    If dr("Dayst").ToString <> "32" Then '不是月底
                        dd1 = DateTime.Parse(Today.Year.ToString + "/02/" + dr("Dayst").ToString)
                        dd2 = DateTime.Parse(Today.Year.ToString + "/05/" + dr("Dayst").ToString)
                        dd3 = DateTime.Parse(Today.Year.ToString + "/08/" + dr("Dayst").ToString)
                        dd4 = DateTime.Parse(Today.Year.ToString + "/11/" + dr("Dayst").ToString)

                    Else '是月底
                        dd1 = DateTime.Parse(Today.Year.ToString + "/02/01").AddMonths(1).AddDays(-1)
                        dd2 = DateTime.Parse(Today.Year.ToString + "/05/01").AddMonths(1).AddDays(-1)
                        dd3 = DateTime.Parse(Today.Year.ToString + "/08/01").AddMonths(1).AddDays(-1)
                        dd4 = DateTime.Parse(Today.Year.ToString + "/11/01").AddMonths(1).AddDays(-1)

                    End If
                End If
                If dr("Monthst").ToString = "3" Then
                    If dr("Dayst").ToString <> "32" Then '不是月底
                        dd1 = DateTime.Parse(Today.Year.ToString + "/03/" + dr("Dayst").ToString)
                        dd2 = DateTime.Parse(Today.Year.ToString + "/06/" + dr("Dayst").ToString)
                        dd3 = DateTime.Parse(Today.Year.ToString + "/09/" + dr("Dayst").ToString)
                        dd4 = DateTime.Parse(Today.Year.ToString + "/12/" + dr("Dayst").ToString)

                    Else '是月底
                        dd1 = DateTime.Parse(Today.Year.ToString + "/03/01").AddMonths(1).AddDays(-1)
                        dd2 = DateTime.Parse(Today.Year.ToString + "/06/01").AddMonths(1).AddDays(-1)
                        dd3 = DateTime.Parse(Today.Year.ToString + "/09/01").AddMonths(1).AddDays(-1)
                        dd4 = DateTime.Parse(Today.Year.ToString + "/12/01").AddMonths(1).AddDays(-1)

                    End If
                End If


                If (dd1 = Today Or dd2 = Today Or dd3 = Today Or dd4 = Today) Then

                    AssetType = dr("AssetType").ToString()
                    DeptNo = dr("DeptNo1").ToString()
                    TakeOfficeDate = dr("TakeOfficeDate").ToString()
                    HouseID = dr("HouseID").ToString()
                    TopN = dr("TopN").ToString()
                    EquQty = dr("EquQty").ToString()
                    MailSubject = dr("MailSubject").ToString()
                    MailBody = dr("MailBody").ToString()
                    ResendDays = Integer.Parse(dr("ResendDays").ToString)
                    ResendTimes = Integer.Parse(dr("ResendTimes").ToString)
                    ReturnDays = Integer.Parse(dr("ReturnDays").ToString)

                    dsGroup = aBBL.GetAssetGroupData(AssetType, DeptNo.TrimEnd("0"), TakeOfficeDate, HouseID, TopN, EquQty, "", "", dbName)
                    For Each dr1 As DataRow In dsGroup.Tables(1).Rows '某一個員工
                        drs = dsGroup.Tables(0).Select("keeping_person='" + dr1("keeping_person").ToString.Trim + "'")
                        Language = uBBL.GetLanguage(dr1("keeping_person").ToString.Trim, dbName)


                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select
                        If MailType.ToLower <> "online" Then '上線
                            email = MailTo
                        Else '測試
                            email = drs(0)("EMail").ToString()
                        End If
                        '送出信件
                        mbbl.SendMailDataSum(drs, WebReply, MailFrom, SMTPServer, email, MailSubject, MailBody, dbName, "", WebQuery, Language)
                    Next

                    ''再新增紀錄到資料庫中(清除已回報資料及新增LOG)

                    For Each dr3 As DataRow In dsGroup.Tables(1).Rows ''每一個UID
                        asDS = aBBL.GetAssetDataByAssettypeUID("", dr3("keeping_person").ToString, dbName) '找出某一個人的所有資產
                        Language = uBBL.GetLanguage(dr3("keeping_person").ToString.Trim, dbName)

                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select
                        ReturnDeadLine = aBBL.GetWorkDate(Today, ReturnDays, dr3("keeping_person").ToString, dbName)
                        For Each dr2 As DataRow In asDS.Tables(0).Rows
                            aBBL.InsertSendMailLogWS(dr2("asset_serialNo").ToString, "Sys", ReturnDeadLine, ResendDays, ResendTimes, MailSubject, MailBody, Integer.Parse(dr("SerNo").ToString), dbName) ''逐筆寫入資料
                        Next

                    Next


                    mbbl.UpdateSendMailStatus(dr("SerNo").ToString, "P", dbName, "Sys")
                End If

            End If


        Next


    End Sub


    Private Sub SendType3()
        '抓出設定檔
        Dim dt As DataTable
        Dim dbName As String = "AUOOAMS"
        Dim dsGroup As DataSet
        Dim mbbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()
        Dim aBBL As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        Dim AssetType, DeptNo, TakeOfficeDate, HouseID, TopN, EquQty As String
        Dim dd As DateTime

        Dim MailSubject, MailBody, Language As String
        Dim email As String

        Dim drs() As DataRow

        Dim asDS As DataSet
        Dim ReturnDeadLine As DateTime
        Dim ReturnDays, ResendDays, ResendTimes As Integer
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()

        dt = mbbl.GetSendMail("", "", "", dbName)
        For Each dr As DataRow In dt.Rows
            If dr("ScheduleType").ToString = "3" And (dr("Status").ToString = "N" Or dr("Status").ToString = "P") Then '每 月
                ''找出是今日的資料
                If dr("Dayst").ToString <> "32" Then '不是月底
                    dd = DateTime.Parse(Today.Year.ToString + "/" + Today.Month.ToString + "/" + dr("Dayst").ToString)
                Else '是月底
                    dd = DateTime.Parse(Today.Year.ToString + "/" + Today.Month.ToString + "/01").AddMonths(1).AddDays(-1)
                End If

                If dd = Today Then

                    AssetType = dr("AssetType").ToString()
                    DeptNo = dr("DeptNo1").ToString()
                    TakeOfficeDate = dr("TakeOfficeDate").ToString()
                    HouseID = dr("HouseID").ToString()
                    TopN = dr("TopN").ToString()
                    EquQty = dr("EquQty").ToString()

                    ResendDays = Integer.Parse(dr("ResendDays").ToString)
                    ResendTimes = Integer.Parse(dr("ResendTimes").ToString)
                    ReturnDays = Integer.Parse(dr("ReturnDays").ToString)

                    dsGroup = aBBL.GetAssetGroupData(AssetType, DeptNo.TrimEnd("0"), TakeOfficeDate, HouseID, TopN, EquQty, "", "", dbName)
                    For Each dr1 As DataRow In dsGroup.Tables(1).Rows '某一個員工
                        drs = dsGroup.Tables(0).Select("keeping_person='" + dr1("keeping_person").ToString.Trim + "'")

                        Language = uBBL.GetLanguage(dr1("keeping_person").ToString.Trim, dbName)


                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select

                        If MailType.ToLower <> "online" Then '上線
                            email = MailTo
                        Else '測試
                            email = drs(0)("EMail").ToString()
                        End If
                        '送出信件
                        mbbl.SendMailDataSum(drs, WebReply, MailFrom, SMTPServer, email, MailSubject, MailBody, dbName, "", WebQuery, Language)
                    Next

                    ''再新增紀錄到資料庫中(清除已回報資料及新增LOG)

                    For Each dr3 As DataRow In dsGroup.Tables(1).Rows ''每一個UID
                        asDS = aBBL.GetAssetDataByAssettypeUID("", dr3("keeping_person").ToString, dbName) '找出某一個人的所有資產

                        ReturnDeadLine = aBBL.GetWorkDate(Today, ReturnDays, dr3("keeping_person").ToString, dbName)
                        Language = uBBL.GetLanguage(dr3("keeping_person").ToString.Trim, dbName)

                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select
                        For Each dr2 As DataRow In asDS.Tables(0).Rows
                            aBBL.InsertSendMailLogWS(dr2("asset_serialNo").ToString, "Sys", ReturnDeadLine, ResendDays, ResendTimes, MailSubject, MailBody, Integer.Parse(dr("SerNo").ToString), dbName) ''逐筆寫入資料
                        Next

                    Next


                    mbbl.UpdateSendMailStatus(dr("SerNo").ToString, "P", dbName, "SYS")
                End If

            End If


        Next


    End Sub


    Private Sub SendType4()

        Dim dt As DataTable

        Dim dsGroup As DataSet
        Dim mbbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()
        Dim aBBL As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        Dim AssetType, DeptNo, TakeOfficeDate, HouseID, TopN, EquQty As String
        Dim dd As DateTime

        Dim email, MailSubject, MailBody, Language As String

        Dim drs() As DataRow

        Dim asDS As DataSet
        Dim ReturnDeadLine As DateTime
        Dim ReturnDays, ResendDays, ResendTimes As Integer
        Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()

        dt = mbbl.GetSendMail("", "", "", dbName)

        For Each dr As DataRow In dt.Rows


            If dr("ScheduleType").ToString = "4" And dr("Status").ToString = "N" Then '�榸

                '        ''��X�O���骺���
                Try
                    dd = DateTime.Parse(dr("SendDate").ToString)
                Catch ex As Exception
                    dd = DateTime.Parse("1999/12/31")
                End Try

                If dd = Today Then

                    AssetType = dr("AssetType").ToString()
                    DeptNo = dr("DeptNo1").ToString()
                    TakeOfficeDate = dr("TakeOfficeDate").ToString()
                    HouseID = dr("HouseID").ToString()
                    TopN = dr("TopN").ToString()
                    EquQty = dr("EquQty").ToString()

                    ResendDays = Integer.Parse(dr("ResendDays").ToString)
                    ResendTimes = Integer.Parse(dr("ResendTimes").ToString)
                    ReturnDays = Integer.Parse(dr("ReturnDays").ToString)
                    'MailSubject = dr("MailSubject").ToString()
                    'MailBody = dr("MailBody").ToString()
                    dsGroup = aBBL.GetAssetGroupData(AssetType, DeptNo.TrimEnd("0"), TakeOfficeDate, HouseID, TopN, EquQty, "", "", dbName)
                    For Each dr1 As DataRow In dsGroup.Tables(1).Rows '�Y�@�ӭ��u
                        drs = dsGroup.Tables(0).Select("keeping_person='" + dr1("keeping_person").ToString.Trim + "'")
                        Language = uBBL.GetLanguage(dr1("keeping_person").ToString.Trim, dbName)

                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select

                        If MailType.ToLower <> "online" Then
                            email = MailTo
                        Else
                            email = drs(0)("EMail").ToString()
                        End If

                        mbbl.SendMailDataSum(drs, WebReply, MailFrom, SMTPServer, email, MailSubject, MailBody, dbName, "", WebQuery, Language)
                    Next


                    For Each dr3 As DataRow In dsGroup.Tables(1).Rows ''�C�@��UID
                        asDS = aBBL.GetAssetDataByAssettypeUID("", dr3("keeping_person").ToString, dbName) '��X�Y�@�ӤH���Ҧ��겣
                        Language = uBBL.GetLanguage(dr3("keeping_person").ToString.Trim, dbName)

                        Select Case Language
                            Case "en-us"
                                MailSubject = dr("MailSubjectEng").ToString()
                                MailBody = dr("MailBodyEng").ToString()
                            Case "zh-cn"
                                MailSubject = dr("MailSubjectChn").ToString()
                                MailBody = dr("MailBodyChn").ToString()
                            Case Else
                                MailSubject = dr("MailSubject").ToString()
                                MailBody = dr("MailBody").ToString()
                        End Select
                        ReturnDeadLine = aBBL.GetWorkDate(Today, ReturnDays, dr3("keeping_person").ToString, dbName)
                        For Each dr2 As DataRow In asDS.Tables(0).Rows
                            aBBL.InsertSendMailLogWS(dr2("asset_serialNo").ToString, "Sys", ReturnDeadLine, ResendDays, ResendTimes, MailSubject, MailBody, Integer.Parse(dr("SerNo").ToString), dbName) ''�v���g�J���
                        Next

                    Next


                    mbbl.UpdateSendMailStatus(dr("SerNo").ToString, "Y", dbName, "SYS")
                End If



            End If


        Next


    End Sub




    Public Sub SendBitthDayMail()
        Dim dt As DataTable
        Dim uDao As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
        dt = uDao.GetBirthdayData()
        For Each dr As DataRow In dt.Rows
            SendBirthDayMailDetail(dr("emp_no").ToString, "SYS")

        Next
        baDAO.ErrorLog("SSIS:SendBitthDayMail", "SendBitthDayMail-OK", "SendBitthDayMail", "SYS", "1", dbName)
    End Sub

    Public Sub SendBirthDayMailDetail(ByVal empNo As String, ByVal UID As String)


        Dim bbl As AssetManage.BBL.MailBBL = New AssetManage.BBL.MailBBL()

        Dim abbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        '取得符合條件的資產合計資料
        Dim ds As DataSet = abbl.GetAssetGroupData("", "", "", "0", "", "", empNo, "", dbName)

        Dim MailType As String = System.Configuration.ConfigurationManager.AppSettings("MailType")

        Dim email, MailTo As String
        MailTo = System.Configuration.ConfigurationManager.AppSettings("MailTo")
        Dim MailFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
        Dim SMTPServer As String = System.Configuration.ConfigurationManager.AppSettings("SMTPServer")

        'Dim rootDir As String = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/")
        Dim rootDir As String = System.Configuration.ConfigurationManager.AppSettings("rootDir")
        Dim drs() As DataRow
        Dim WebReply As String = System.Configuration.ConfigurationManager.AppSettings("WebReply")
        Dim WebQuery As String = System.Configuration.ConfigurationManager.AppSettings("WebQuery")
        Dim uBBl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        Dim mParBBl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim drParam As DataRow = mParBBl.GetMailParamByID("2", dbName).Rows(0)

        ''先送信
        For Each dr As DataRow In ds.Tables(1).Rows '某一個員工
            drs = ds.Tables(0).Select("keeping_person='" + dr("keeping_person").ToString.Trim + "'")
            If MailType.ToLower <> "online" Then '測試
                email = MailTo
            Else '上線
                email = drs(0)("EMail").ToString()
            End If
            '送出信件
            ''配合語文別
            Select Case uBBl.GetLanguage(dr("keeping_person").ToString.ToLower(), dbName)
                Case "en-us"
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailSubjectEng").ToString, drParam("MailBodyEng").ToString(), dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "en-us")
                Case "zh-cn"
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailsubjectChn").ToString, drParam("MailBodyChn").ToString, dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-cn")
                Case Else
                    bbl.SendMailDataSum(drs, rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebReply, MailFrom, SMTPServer, email, drParam("MailSubject").ToString, drParam("MailBody").ToString, dbName, "", rootDir + "/WebForm/Main/MainPage.aspx?path=" + rootDir + WebQuery, "zh-tw")
            End Select
        Next
        Dim ResendDays As Integer = Integer.Parse(drParam("ResendDays").ToString)
        Dim ResendTimes As Integer = Integer.Parse(drParam("ResendTimes").ToString)
        Dim ReturnDays As Integer = Integer.Parse(drParam("ReturnDays").ToString)
        Dim ReturnDeadLine As DateTime

        ''再新增紀錄到資料庫中(清除已回報資料及新增LOG)
        Dim asDS As DataSet
        For Each dr1 As DataRow In ds.Tables(1).Rows ''每一個UID
            asDS = abbl.GetAssetDataByAssettypeUID("", dr1("keeping_person").ToString, dbName) '找出某一個人的所有資產

            ReturnDeadLine = abbl.GetWorkDate(Today, ReturnDays, dr1("keeping_person").ToString, dbName)
            For Each dr2 As DataRow In asDS.Tables(0).Rows
                Select Case uBBl.GetLanguage(dr1("keeping_person").ToString.ToLower(), dbName)
                    Case "en-us"
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailSubjectEng").ToString, drParam("MailBodyEng").ToString, 0, dbName) ''逐筆寫入資料

                    Case "zh-tw"
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailSubject"), drParam("MailBody").ToString, 0, dbName) ''逐筆寫入資料

                    Case Else
                        abbl.InsertSendMailLog(dr2("asset_SerialNo").ToString, UID, ReturnDeadLine, ResendDays, ResendTimes, drParam("MailsubjectCht").ToString, drParam("MailBodyChn").ToString, 0, dbName) ''逐筆寫入資料


                End Select

            Next

        Next



    End Sub











End Class
